 // pages/rentalsubmit/rentalsubmit.js

var QQMapWX = require('../../qqmap-wx-jssdk.min.js');
 
// 实例化API核心类
var qqmapsdk = new QQMapWX({
    key: '6FTBZ-OPECR-OYVWT-WULAV-V7VPQ-FAFCT' // 必填
});

Page({
  data: {
    //标题
    title:'',
    //文本介绍
    intro:'',
    //图片
    picurls:[],
    //房屋地址  
    address:'',
    area:'',
    size:[['1室','2室','3室','4室','5室'],
          ['0厅','1厅','2厅','3厅'],
          ['0卫','1卫','2卫','3卫']],
    rentmoney:'',
    index:[0,0,0],
    devicechoose:false,
    device:[
      {choose:false,name:'热水器'},
      {choose:false,name:'衣柜'},
      {choose:false,name:'空调'},
      {choose:false,name:'暖气'},
      {choose:false,name:'洗衣机'},
      {choose:false,name:'电视机'},
      {choose:false,name:'燃气灶'},
      {choose:false,name:'WIFI'},
      {choose:false,name:'独立卫生间'},
      {choose:false,name:'大阳台'},
      {choose:false,name:'微波炉'},
      {choose:false,name:'冰箱'}],
      phone:''
  },
   //获取输入信息
   title(e){
    this.setData({
      title:e.detail.value
    })
  },
  intro(e){
    this.setData({
      intro:e.detail.value
    })
  },
  //添加照片
  selectpic(e){
    let that = this;
    wx.chooseImage({
      count: 9,
      success(res){
        var picurls = that.data.picurls.concat(res.tempFilePaths)
        that.setData({
          picurls:picurls
        })
      }
    })
  },
  //删除照片
  delpic(e){
    // console.log(e)
    let that = this;
    var index = e.currentTarget.dataset.index;
    var del = this.data.picurls
    wx.showModal({
      content: '是否删除该照片',
      title: '提示',
      success(res){
        if(res.confirm){
          del.splice(index,1)
          that.setData({
            picurls:del
          })
      // console.log(this.data.picurls)
        }
      },
    })
  },
  // 查看图片
  showpic(e){
    var index  =e.currentTarget.dataset.index;
    wx.previewImage({
      current: this.data.picurls[index],     //当前图片地址
      urls: this.data.picurls,                 //所有要预览的图片的地址集合 数组形式
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  area(e){
    this.setData({
      area:e.detail.value
    })
  },
  address(e){
    this.setData({
      address:e.detail.value
    })
  },
  //展示地址位置
  showaddress(e){
    var _this = this;
    //调用地址解析接口
    qqmapsdk.geocoder({
      //获取表单传入地址
      address: _this.data.address, //地址参数，例：固定地址，address: '北京市海淀区彩和坊路海淀西大街74号'
      success: function(res) {//成功后的回调
        // console.log(res);
        var res = res.result;
        var latitude = res.location.lat;
        var longitude = res.location.lng;
        //根据地址解析在地图上标记解析地址位置
        _this.setData({ // 获取返回结果，放到markers及poi中，并在地图展示
          markers: [{
            id: 0,
            title: res.title,
            latitude: latitude,
            longitude: longitude,
            iconPath: '/pic/地标.png',//图标路径
            width: 30,
            height: 30,
            // callout: { //可根据需求是否展示经纬度
            //   content: latitude + ',' + longitude,
            //   color: '#000',
            //   display: 'ALWAYS'
            // }
          }],
          poi: { //根据自己data数据设置相应的地图中心坐标变量名称
            latitude: latitude,
            longitude: longitude
          },
        });

        wx.openLocation({
          latitude:latitude,
          longitude:longitude,
          address: _this.data.address,
          name: _this.data.address,
          scale: 28,
        })
      },
      fail: function(error) {
        console.error(error);
      },
      complete: function(res) {
        console.log(res);
      }
    })
  },
  choosetype(e){
    // console.log(e)
    this.setData({
      index:e.detail.value
    })
  },
  rentmoney(e){
    this.setData({
      rentmoney:e.detail.value
    })
  },
  devicechoose(){
    this.setData({
      devicechoose2:true
    })
  },
  choosecancel(){
    this.setData({
      devicechoose:true,
      devicechoose2:false
    })
  },
  choosedevice(e){
    var index = e.currentTarget.dataset.index
    // console.log(index)
    var choose = 'device['+index+'].choose';
    var value = this.data.device[index].choose;
    this.setData({
      [choose]:!value
    })
  },
  // 获取电话
  phone(e){
    this.setData({
      phone:e.detail.value
    })
  },
  //发布租房信息
  submit(e){
    var that = this;
    // console.log(this.data.index)
    wx.showModal({
      title: '确定发布该出租信息吗?',
      complete: (res) => {
        //确定发布出租物品
        if (res.confirm) {
            var arr = [];
            var  h = this.data.picurls.length;
            var i =0;
            for (i = 0; i < h; i++) {
            //上传文件
            // console.log(i,h)
            wx.uploadFile({
              url: 'http://127.0.0.1:8080/image/upload',
              filePath: this.data.picurls[i],
                name: "file",
                header: {
                  "content-type": "multipart/form-data"
                },
                success: function ({ data: res }) {
                  // console.log(res)
                  res = JSON.parse(res);
                  const pic_url = 'http://127.0.0.1:8080/image/download?name=' + res.data
                  // console.log(pic_url)
                  // 图片路径集合
                  arr.unshift(pic_url);
                },
                fail: function (err) {
                  wx.showToast({
                    title: "上传失败",
                    icon: "error",
                    duration: 2000
                  })
                },
                complete: function (result) {
                  // console.log(result.errMsg)
                }
              })
            }
          

            setTimeout(function(){
              //设备信息的字符串
          var string = ''
          for(var i=0;i<that.data.device.length;i++){
            if(that.data.device[i].choose == true)
                string = string +  i + ','
          }
          
          // console.log(string)

          //data中的数据
          var data = that.data;
          //获取当前时间戳
          var item = Date.parse(new Date())/1000;

          var nowtime = new Date().toJSON().substring(0,10)+' '+new Date().toTimeString().substring(0,8); 
          var data1 = {
            rentalid:'r'+item,
            title:data.title,
            intro:data.intro,
            pic:JSON.stringify(arr),
            address:data.address,
            size:JSON.stringify(data.index),
            device:string,
            month:data.rentmoney,
            area:data.area,
            contact:data.phone,
            time:nowtime,
            userid:wx.getStorageSync('openid')
          }
          // console.log(data1)

          wx.request({
            url: 'http://127.0.0.1:8080/rental/addRental',
            method:"POST",
            data:data1,
            success(res){
              // console.log(res)
              if(res.data){
                wx.navigateBack({
                  delta: 1,
                  success: (res) => {
                    wx.showToast({
                      title: '发布成功',
                      icon:"success"
                    })
                  },
                })
              }
            }
          })

            },100)
          

        }
      }
    })
    
  }

  
})